﻿using System;
using System.Collections.Generic;
using System.Text;
using TTServer.Messages;
using System.Threading;

namespace TTServer.Objects
{
    public class CTaskQueue
    {
        private readonly object queueLock = new object();

        private Queue<CTask> m_queue = new Queue<CTask>();

        public void Enqueue(CTask task)
        {
            lock (queueLock)
            {
                if (m_queue.Count == 0)
                {
                    m_queue.Enqueue(task);

                    Monitor.Pulse(queueLock);
                }
                else
                {
                    m_queue.Enqueue(task);
                }
            }
        }

        public CTask Dequeue()
        {
            lock (queueLock)
            {
                while (m_queue.Count == 0)
                {
                    Monitor.Wait(queueLock);
                }

                return m_queue.Dequeue();
            }
        }
    }
}
